package com.njitt.bysj.controller;

import com.google.inject.Module;
import com.njitt.bysj.base.BaseController;
import com.njitt.bysj.base.ResultInfo;
import com.njitt.bysj.bean.PermissionBean;
import com.njitt.bysj.srevice.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/26 10:17
 */
@Controller
@RequestMapping("permission")
public class PermissionController extends BaseController {

    @Resource
    private PermissionService permissionService;

    @ResponseBody
    @RequestMapping("item")
    public Map<String, Object> item() {
        Map<String, Object> map = new LinkedHashMap<>();
        Map<String, Object> homeInfo = new LinkedHashMap<>();
        homeInfo.put("title", "首页");
        homeInfo.put("href", "/erp/welcome");
        map.put("homeInfo", homeInfo);

        Map<String, Object> logoInfo = new LinkedHashMap<>();
        logoInfo.put("title", "erp");
        logoInfo.put("image", "layuimini/images/logo.png");
        logoInfo.put("href", "");
        map.put("logoInfo", logoInfo);
        List<Map<String, Object>> menuInfo = permissionService.selectAllMenu();
        map.put("menuInfo", menuInfo);
        return map;
    }

    @RequestMapping("roleItem")
    @ResponseBody
    public Map<String, Object> roleItem() {
        Map<String, Object> roleMap = new LinkedHashMap<>();
        Map<String, Object> homeInfo = new LinkedHashMap<>();
        homeInfo.put("title", "首页");
        homeInfo.put("href", "/erp/welcome");
        roleMap.put("homeInfo", homeInfo);
        Map<String, Object> logoInfo = new LinkedHashMap<>();
        logoInfo.put("title", "erp");
        logoInfo.put("image", "layuimini/images/logo.png");
        logoInfo.put("href", "");
        roleMap.put("logoInfo", logoInfo);
        List<Map<String, Object>> menuItem = permissionService.roleMenu();
        roleMap.put("menuInfo", menuItem);
        return roleMap;
    }

    @RequestMapping("module")
    @ResponseBody
    public Map<String, Object> queryAllModule() {
        return permissionService.queryAllModule();
    }

    @RequestMapping("toAddModulePage")
    public String toAddModulePage(Integer pId, Integer isType, HttpServletRequest request) {
        request.setAttribute("isType", isType);
        request.setAttribute("pId", pId);
        return "module/add";
    }

    @RequestMapping("addModule")
    @ResponseBody
    public ResultInfo addModule(PermissionBean permissionBean) {
        permissionService.addModule(permissionBean);
        return success("success");
    }

    @RequestMapping("toUpdateModulePage")
    public String toUpdateModulePage(Integer id, HttpServletRequest request) {
        // 将需要修改的资源对象设置到请求域中
        request.setAttribute("permission", permissionService.selectByPrimaryKey(id));
        return "module/update";
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo update(PermissionBean permissionBean) {
        permissionService.updateModule(permissionBean);
        return success("success");
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer id) {
        permissionService.deleteModule(id);
        return success("success");
    }

}
